﻿using Fierce.Core.Configuration;
using Fierce.Dapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;


namespace Fierce.Core.Context
{
    internal partial class EntitiesContext : DbContext
    {
        /// <summary>
        /// 数据连接
        /// </summary>
        internal IDbConnection Connection { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="connString"></param>
        /// <param name="providerName"></param>
        internal EntitiesContext(string connString, string providerName)
        {
            Connection = DbFactory.GetDbConnection(connString, providerName);
        }

        /// <summary>
        /// 注册数据库
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(Connection as DbConnection, b => b.UseRowNumberForPaging());
        }

        /// <summary>
        /// 实体注册
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 初始化实体模型生成器
            EntityConfigurationFactory.ConfigurationsInit(modelBuilder);
        }
    }
}